
import { Component, ViewChild, Injector, Output, EventEmitter, ElementRef, ViewEncapsulation } from '@angular/core';
import { ModalDirective } from 'ngx-bootstrap';
import { AppComponentBase } from '@shared/common/app-component-base';
import { LocationServiceProxy, TechFileServiceProxy, LocationDto, TechFileListDto, TechFileBaseInputDto } from '@shared/service-proxies/service-proxies';
import { finalize } from 'rxjs/operators';

@Component({
  selector: 'app-receive-techfile-modal',
  templateUrl: './receive-techfile-modal.component.html',
  encapsulation: ViewEncapsulation.None,
  // styleUrls: ['./receive-techfile-modal.component.less']
})

export class ReceiveTechfileModalComponent extends AppComponentBase {

  @ViewChild('actionModal') modal: ModalDirective;

  @ViewChild('areaCombobox') areaCombobox: ElementRef;
  @ViewChild('shelfCombobox') shelfCombobox: ElementRef;
  @ViewChild('positionCombobox') positionCombobox: ElementRef;

  @Output() modalSave: EventEmitter<any> = new EventEmitter<any>();

  active = false;
  saving = false;

  activeIndex = 0;

  releaseStockBill = new TechFileListDto();

  areas = new Array<LocationDto>();
  shelfs = new Array<LocationDto>();
  positions = new Array<LocationDto>();
  locations: any[];
  hasLoadLocations = false;

  areaId = 0;
  shelfId = 0;
  positionId = 0;

  items = [
    { label: '数据校验' },
    { label: '位置选择' }
  ];

  constructor(
    injector: Injector,
    private _techFileServiceProxy: TechFileServiceProxy,
    private _locationServiceProxy: LocationServiceProxy,

  ) {
    super(injector);
  }

  show(bill: TechFileListDto): void {
    this.activeIndex = 0;

    this.areaId = 0;
    this.shelfId = 0;
    this.positionId = 0;

    this.active = true;
 
    this.releaseStockBill = bill;

    setTimeout(() => {
      $(this.areaCombobox.nativeElement).selectpicker('val');
      $(this.shelfCombobox.nativeElement).selectpicker('val');
      $(this.positionCombobox.nativeElement).selectpicker('val');
      $(this.areaCombobox.nativeElement).selectpicker('refresh');
      $(this.shelfCombobox.nativeElement).selectpicker('refresh');
      $(this.positionCombobox.nativeElement).selectpicker('refresh');

      this.modal.show();
    }, 0);



  }

  onNextBtnClick() {
    this.activeIndex = 1;  
    if (!this.hasLoadLocations) {
      this._locationServiceProxy.getAreas()
        .subscribe(result => {
          this.areas = result.items;
          setTimeout(() => {
            if (this.releaseStockBill.positionId != null && this.releaseStockBill.positionId != 0) {
              this.load();
            } else 
            {
              $(this.areaCombobox.nativeElement).selectpicker('val');
              $(this.shelfCombobox.nativeElement).selectpicker('val');
              $(this.positionCombobox.nativeElement).selectpicker('val');
              $(this.areaCombobox.nativeElement).selectpicker('refresh');
              $(this.shelfCombobox.nativeElement).selectpicker('refresh');
              $(this.positionCombobox.nativeElement).selectpicker('refresh');
            }
          }, 0);
        });
    }
  }

  load() {

    this._locationServiceProxy.getLocationIdsByPositionId(this.releaseStockBill.positionId)
      .subscribe(result => {

        if (result != "") {
          this.locations = result.split(',');

        }
        setTimeout(() => {
          $(this.areaCombobox.nativeElement).selectpicker('val', this.locations[0]);
          $(this.areaCombobox.nativeElement).selectpicker('refresh');
          this.loadShelfs2(this.locations);
        }, 0);
      });
  }

  loadShelfs2(ids: any[]) {
    this.shelfId = 0;
    this.positionId = 0;

    this._locationServiceProxy.getShelvesByArea(ids[0])
      .subscribe(result => {
        this.shelfs = result.items;
        setTimeout(() => {
          $(this.shelfCombobox.nativeElement).selectpicker('val', ids[1]);
          $(this.shelfCombobox.nativeElement).selectpicker('refresh');
          this.loadPositions2(ids);
        }, 0);
      });
  }


  loadPositions2(ids: any[]) {
    this.positionId = 0;

    this._locationServiceProxy.getPositionsByShelf(ids[1])
      .subscribe(result => {
        this.positions = result.items;
        setTimeout(() => {
          this.positionId = ids[2];
          $(this.positionCombobox.nativeElement).selectpicker('val', ids[2]);
          $(this.positionCombobox.nativeElement).selectpicker('refresh');
        }, 0);
      });
  }


  loadShelfs() {
    this.shelfId = 0;
    this.positionId = 0;

    this._locationServiceProxy.getShelvesByArea(this.areaId)
      .subscribe(result => {
        this.shelfs = result.items;
        setTimeout(() => {
          $(this.shelfCombobox.nativeElement).selectpicker('refresh');
        }, 0);
      });
  }

  loadPositions() {
    this.positionId = 0;

    this._locationServiceProxy.getPositionsByShelf(this.shelfId)
      .subscribe(result => {
        this.positions = result.items;
        setTimeout(() => {
          $(this.positionCombobox.nativeElement).selectpicker('refresh');
        }, 0);
      });
  }

  save(): void {
    this.saving = true;

    let input = new TechFileBaseInputDto();
    input.ids=  new Array<string>(this.releaseStockBill.id);  
    input.positionId = this.positionId;
    input.remarks = this.releaseStockBill.remarks;
    this._techFileServiceProxy.receiveTechFile(input)
      .pipe(finalize(() => {
        this.saving = false;
      }))
      .subscribe((result) => {
        this.close();
        this.modalSave.emit(null);
        this.notify.success('接收成功');
      });
  }

  update(): void {
    this.saving = true;

    let input = new TechFileBaseInputDto();
     
    input.ids=  new Array<string>(this.releaseStockBill.id);  
    input.positionId = this.positionId;
    input.remarks = this.releaseStockBill.remarks;
    this._techFileServiceProxy.updateTechFileLocation(input)
      .pipe(finalize(() => {
        this.saving = false;
      }))
      .subscribe((result) => {
        this.close();
        this.notify.success('保存成功'); 
        this.modalSave.emit(null);
       
      });
  }

  close(): void {
    this.saving = false;
    this.modal.hide();
  }
}
